package com.tusiyou.homework;

import android.app.Service;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;


public class SearchFragment extends Fragment {

    private PopupWindow popupWindow;

    public SearchFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        popupWindow = new PopupWindow();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_search, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final Toolbar tb = (Toolbar) view.findViewById(R.id.search_toolbar);
        final LinearLayout ll = (LinearLayout) view.findViewById(R.id.search_fargment);
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹出浮动框
                //封装代码(以LinearLayout作为参照，所以作为入参传递)；

                showPopUpWindow(ll,tb);

                //弹出输入法
                //输入法系统服务
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Service.INPUT_METHOD_SERVICE);
                //如果当前没有显示软输入法键盘可以显示
                //第一个参数为显示的flag
                //第二个参数为隐藏的flag
                imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        });
    }



    private void showPopUpWindow(LinearLayout ll,Toolbar toolbar) {
        if(!popupWindow.isShowing()){
            //进行实际的显示
            //1、设置popupWindow的属性
            // 宽度、高度
            //就是toolbar的宽度和高度
            popupWindow.setWidth(toolbar.getWidth());
            popupWindow.setHeight(toolbar.getHeight());

            //设置popupWindow自己的视图

            View v = getActivity().getLayoutInflater().inflate(R.layout.popupwindow_search,null);
            TextView tvCancel = (TextView) v.findViewById(R.id.popupwindow_cancel);
            //设置textView的监听，点击则取消popupWindow
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    popupWindow.dismiss();
                }
            });

            popupWindow.setContentView(v);
            popupWindow.setOutsideTouchable(true);//设置在外部可点击，点击以后可以取消
            popupWindow.setFocusable(true);//获取聚焦，才可以在EditText填写内容

            //一般情况，Y轴进行偏移(偏移状态栏的高度)


            int dimenY = getStatusHeight();
            popupWindow.showAtLocation(ll, Gravity.TOP,0,dimenY);
        }
    }

    //获取状态栏的高度
    public int getStatusHeight(){
        int resId;//获取状态栏标量对应的地址
        resId = getResources().getIdentifier("status_bar_height","dimen","android");

        if(resId>0){
            //通过地址获取尺寸
            return getResources().getDimensionPixelOffset(resId);
        }

        return 0;
    }
}
